package com.hpe.fastfood.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hpe.fastfood.pojo.Bussiness;
import com.hpe.fastfood.pojo.Comment;
import com.hpe.fastfood.pojo.Commodity;
import com.hpe.fastfood.pojo.Type;
import com.hpe.fastfood.service.BussinessService;
import com.hpe.fastfood.service.CommodityService;
import com.hpe.fastfood.service.OrderService;
import com.hpe.fastfood.service.TypeService;
import com.hpe.fastfood.service.UserService;
import com.hpe.fastfood.util.Page;
import com.hpe.fastfood.vo.CommodityVo;
import com.hpe.fastfood.vo.OrderVo;

/**
 * @ClassName: ShopController
 * @Description:TODO 商品的controller
 * @author: fuchong
 * @date: 2018年11月25日 下午1:58:53
 */
@Controller
@RequestMapping("shop")
public class ShopController {
	@Autowired
	private CommodityService commodityService;

	@Autowired
	private TypeService typeService;

	@Autowired
	private BussinessService bussinessService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private UserService userService;

	/**
	 * @Description:TODO 查询商品信息
	 * @author: fuchong
	 * @date: 2018年11月25日 下午3:18:30
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "selectShop")
	public ModelAndView selectShop(Page page, String foodName, Integer typeId) {
		ModelAndView mv = new ModelAndView();
		List<Type> type = null;
		try {
			type = typeService.selectTypeInfo();
			page = commodityService.selectShop(page, foodName, typeId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		mv.addObject("foodName", foodName);
		mv.addObject("type", type);
		mv.addObject("page", page);
		mv.setViewName("category");
		return mv;
	}

	/**
	 * @Description:TODO 根据id查询商品信息
	 * @author: fuchong
	 * @date: 2018年11月25日 下午7:27:50
	 * @param foodId
	 * @return
	 */
	@RequestMapping(value = "selectShopById")
	public ModelAndView selectShopById(int foodId) {
		ModelAndView mv = new ModelAndView();
		CommodityVo commodity = null;
		List<OrderVo> order = null;
		List<Comment> comment = null;
		int count = 0;
		int sum = 0;
		int countcomment = 0;
		try {
			commodity = commodityService.selectCommodityById(foodId);// 查询商品信息
			order = orderService.selectOrderById(foodId);// 查询订单信息
			count = orderService.selectCountById(foodId);// 查询订单数量
			sum = orderService.sumFoodCountById(foodId);// 查询销售总量
			comment = userService.selectCommentByfoodId(foodId);// 查询评论
			countcomment = userService.selectCommentCountByfoodId(foodId);// 查询评论
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		mv.addObject("commodity", commodity);
		mv.addObject("order", order);
		mv.addObject("ordercount", count);
		mv.addObject("orderSumCount", sum);
		mv.addObject("comment", comment);
		mv.addObject("countcomment", countcomment);
		mv.setViewName("detailsp");
		return mv;
	}

	/**
	 * @Description:TODO 根据id查询商家
	 * @author: fuchong
	 * @date: 2018年11月26日 上午9:26:12
	 * @param foodId
	 * @return
	 */
	@RequestMapping(value = "selectBussinessByBussId")
	public ModelAndView selectBussinessById(int foodId) {
		ModelAndView mv = new ModelAndView();
		CommodityVo bussiness = null;
		List<Commodity> shops = null;
		try {
			bussiness = commodityService.selectCommodityById(foodId);// 查询商家信息
			shops = commodityService.selectCommodityByBussId(bussiness.getBussinessId());// 查询该商店的所有商品
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		mv.addObject("bussiness", bussiness);
		mv.addObject("shops", shops);
		mv.setViewName("shop");
		return mv;
	}

}
